package com.novell.sasl.client;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.harmony.javax.security.sasl.SaslException;

class DirectiveList
{
  private static final int STATE_LOOKING_FOR_COMMA = 6;
  private static final int STATE_LOOKING_FOR_DIRECTIVE = 2;
  private static final int STATE_LOOKING_FOR_EQUALS = 4;
  private static final int STATE_LOOKING_FOR_FIRST_DIRECTIVE = 1;
  private static final int STATE_LOOKING_FOR_VALUE = 5;
  private static final int STATE_NO_UTF8_SUPPORT = 9;
  private static final int STATE_SCANNING_NAME = 3;
  private static final int STATE_SCANNING_QUOTED_STRING_VALUE = 7;
  private static final int STATE_SCANNING_TOKEN_VALUE = 8;
  private String m_curName;
  private int m_curPos = 0;
  private ArrayList m_directiveList;
  private String m_directives;
  private int m_errorPos;
  private int m_scanStart;
  private int m_state = 1;

  DirectiveList(byte[] paramArrayOfByte)
  {
    this.m_directiveList = new ArrayList(10);
    this.m_scanStart = 0;
    this.m_errorPos = -1;
    try
    {
      this.m_directives = new String(paramArrayOfByte, "UTF-8");
      return;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      this.m_state = 9;
    }
  }

  void addDirective(String paramString, boolean paramBoolean)
  {
    String str;
    int k;
    if (!(paramBoolean))
    {
      str = this.m_directives.substring(this.m_scanStart, this.m_curPos);
      if (this.m_state != 7)
        break label143;
      k = 1;
    }
    while (true)
    {
      this.m_directiveList.add(new ParsedDirective(paramString, str, k));
      return;
      StringBuffer localStringBuffer = new StringBuffer(this.m_curPos - this.m_scanStart);
      int i = 0;
      int j = this.m_scanStart;
      while (true)
      {
        while (j >= this.m_curPos)
          str = new String(localStringBuffer);
        if ('\\' == this.m_directives.charAt(j));
        localStringBuffer.setCharAt(i, this.m_directives.charAt(++j));
        ++i;
        ++j;
      }
      label143: k = 2;
    }
  }

  Iterator getIterator()
  {
    return this.m_directiveList.iterator();
  }

  boolean isValidTokenChar(char paramChar)
  {
    int i;
    if (((paramChar >= 0) && (paramChar <= ' ')) || ((paramChar >= ':') && (paramChar <= '@')) || ((paramChar >= '[') && (paramChar <= ']')) || (',' == paramChar) || ('%' == paramChar) || ('(' == paramChar) || (')' == paramChar) || ('{' == paramChar) || ('}' == paramChar) || ('' == paramChar))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  boolean isWhiteSpace(char paramChar)
  {
    int i;
    if (('\t' == paramChar) || ('\n' == paramChar) || ('\r' == paramChar) || (' ' == paramChar))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  void parseDirectives()
    throws SaslException
  {
    boolean bool = false;
    String str = "<no name>";
    if (this.m_state == 9)
      throw new SaslException("No UTF-8 support on platform");
    int i = 0;
    if (this.m_curPos >= this.m_directives.length())
      if (0 == 0);
    switch (this.m_state)
    {
    case 1:
    case 6:
    default:
    case 8:
      while (true)
      {
        return;
        char c = this.m_directives.charAt(this.m_curPos);
        switch (this.m_state)
        {
        default:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 8:
        case 7:
        case 6:
        }
        while (true)
        {
          do
            while (true)
            {
              do
              {
                while (true)
                {
                  while (true)
                  {
                    do
                    {
                      while (true)
                      {
                        while (true)
                        {
                          do
                          {
                            while (true)
                            {
                              do
                              {
                                while (true)
                                {
                                  while (true)
                                  {
                                    do
                                    {
                                      while (true)
                                      {
                                        do
                                          while (true)
                                          {
                                            if (0 != 0);
                                            i = c;
                                            this.m_curPos = (1 + this.m_curPos);
                                          }
                                        while (isWhiteSpace(c));
                                        if (!(isValidTokenChar(c)))
                                          break;
                                        this.m_scanStart = this.m_curPos;
                                        this.m_state = 3;
                                      }
                                      this.m_errorPos = this.m_curPos;
                                      throw new SaslException("Parse error: Invalid name character");
                                    }
                                    while (isValidTokenChar(c));
                                    if (!(isWhiteSpace(c)))
                                      break;
                                    str = this.m_directives.substring(this.m_scanStart, this.m_curPos);
                                    this.m_state = 4;
                                  }
                                  if ('=' != c)
                                    break;
                                  str = this.m_directives.substring(this.m_scanStart, this.m_curPos);
                                  this.m_state = 5;
                                }
                                this.m_errorPos = this.m_curPos;
                                throw new SaslException("Parse error: Invalid name character");
                              }
                              while (isWhiteSpace(c));
                              if ('=' != c)
                                break;
                              this.m_state = 5;
                            }
                            this.m_errorPos = this.m_curPos;
                            throw new SaslException("Parse error: Expected equals sign '='.");
                          }
                          while (isWhiteSpace(c));
                          if ('"' != c)
                            break;
                          this.m_scanStart = (1 + this.m_curPos);
                          this.m_state = 7;
                        }
                        if (!(isValidTokenChar(c)))
                          break;
                        this.m_scanStart = this.m_curPos;
                        this.m_state = 8;
                      }
                      this.m_errorPos = this.m_curPos;
                      throw new SaslException("Parse error: Unexpected character");
                    }
                    while (isValidTokenChar(c));
                    if (!(isWhiteSpace(c)))
                      break;
                    addDirective(str, false);
                    this.m_state = 6;
                  }
                  if (',' != c)
                    break;
                  addDirective(str, false);
                  this.m_state = 2;
                }
                this.m_errorPos = this.m_curPos;
                throw new SaslException("Parse error: Invalid value character");
                if ('\\' == c)
                  bool = true;
              }
              while (('"' != c) || (92 == i));
              addDirective(str, bool);
              bool = false;
              this.m_state = 6;
            }
          while (isWhiteSpace(c));
          if (c != ',')
            break;
          this.m_state = 2;
        }
        this.m_errorPos = this.m_curPos;
        throw new SaslException("Parse error: Expected a comma.");
        addDirective(str, false);
      }
    case 2:
      throw new SaslException("Parse error: Trailing comma.");
    case 3:
    case 4:
    case 5:
      throw new SaslException("Parse error: Missing value.");
    case 7:
    }
    throw new SaslException("Parse error: Missing closing quote.");
  }
}